<template>
  <div class="peisong_main">
    <baidu-map
      id="SGbaidumap"
      ref="map"
      class="map"
      :center="center"
      :zoom="zoom"
      :scroll-wheel-zoom="true"
      @ready="handler"
      @click="infoWindowClose"
    >
      <bm-point-collection :points="markerList" shape="BMAP_POINT_SHAPE_WATERDROP" color="red" size="BMAP_POINT_SIZE_BIG" @click="infoWindowOpen" />
      <bm-label v-for="(item, index) in markerList" :key="index" :content="item.name" :position="{lng: item.lng, lat: item.lat}" :offset="{width: -10, height: 20}" :label-style="{color: 'red', fontSize : '14px', padding: '3px'}" @click="getlocation(item)" />
      <div v-if="show" class="info-box">
        <div class="box-top">
          <div style="display: inline-block; margin-bottom: 10px;">待派订单：</div>
          <div @click="infoWindowClose"><i class="el-icon-close" /></div>
        </div>
        <lb-table
          ref="lbTable"
          class="table"
          border
          element-loading-text="加载中"
          :column="tableData02.column"
          :data="tableData02.data"
          height="100"
          style="overflow: auto;"
        />
        <br>
        <span style="display: inline-block; margin-bottom: 10px;">实时库存明细：</span>
        <lb-table
          ref="lbTable"
          class="table"
          border
          element-loading-text="加载中"
          :column="tableData03.column"
          :data="tableData03.data"
          height="200"
        />
      </div>
      <bm-map-type :map-types="['BMAP_NORMAL_MAP', 'BMAP_HYBRID_MAP']" anchor="BMAP_ANCHOR_TOP_RIGHT" />
    </baidu-map>
    <div ref="right" class="right">
      <span style="display: inline-block; height: 16px; margin-bottom: 10px; font-size: 14px; font-weight: bold; color: #777;">配送员列表</span>
      <i class="el-icon-d-arrow-right" @click="fold('right')" />
      <el-card class="rightCard">
        <el-form ref="form" class="form" label-width="0px" style="padding: 5px;">
          <el-form-item class="merge-item">
            <el-select v-model="qtype" style="width: 100px; margin-right: 2px;">
              <el-option :value="1" label="姓名" />
              <el-option :value="2" label="电话" />
            </el-select>
            <el-input v-model.trim="txt" style="width: 190px; margin-right: 2px;" maxlength="11" placeholder="请输入" @input="inputCheck()" @keyup.enter.native="search" />
            <el-button v-permission="[50102, 1]" type="primary" icon="el-icon-search" @click="search">查询</el-button>
          </el-form-item>
        </el-form>
        <lb-table
          ref="lbTable"
          class="table"
          border
          height="100%"
          highlight-current-row
          element-loading-text="加载中"
          :column="tableDataRight.column"
          :data="tableDataRight.data"
          style="overflow: auto;"
          @cell-click="cellClick"
        />
      </el-card>
    </div>

  </div>
</template>

<script>
  import { courierList, courierMapList, courierOrderwinData, companySetting } from '@/api'
  import { parseTime } from '@/utils'
  export default {
    name: 'peisongyuan',
    data() {
      return {
        center: {
          lng: 0,
          lat: 0
        },
        BMap: {},
        map: {},
        show: false,
        qtype: 1,
        txt: '',
        zoom: 14,
        markerList: [],
        tableDataRight: {
          total: 0,
          column: [
            {
              type: 'index',
              align: 'center',
              width: 40
            },
            {
              label: '定位',
              align: 'center',
              width: 50,
              render: (h, { row }) =>
              <span onClick={() => this.getlocation(row)}>
                {row.lat && row.lng && <i class='el-icon-location linkColor' style='cursor:pointer'/>}
              </span>
            },
            {
              prop: 'userName',
              label: '姓名'
            },
            {
              prop: 'uphone',
              label: '电话',
              width: 110
            },
            {
              prop: 'createTime',
              label: '最后定位时间',
              width: 210,
              render: (h, { row }) => <span>{ row.createTime && parseTime(row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }</span>
            }
          ],
          data: []
        },
        tableData02: {
          total: 0,
          column: [
            {
              prop: 'orderCode',
              label: '订单编号'
            },
            {
              prop: 'csmName',
              label: '客户姓名'
            },
            {
              prop: 'csmAddress',
              label: '地址'
            },
            {
              prop: 'csmPhone',
              label: '电话'
            }
          ],
          data: []
        },
        tableData03: {
          total: 0,
          column: [
            {
              prop: 'gtName',
              label: '商品类型'
            },
            {
              prop: 'goodsName',
              label: '商品名称',
              width: 150
            },
            {
              prop: 'gdStatus',
              label: '状态',
              render: (h, { row }) => <span>{row.gdStatus === 1 ? '重' : '轻'}</span>
            },
            {
              prop: 'gdCount',
              align: 'right',
              label: '数量'
            }
          ],
          data: []
        }
      }
    },
    mounted() {
      this.search()
      // this.interval = setInterval(() => {
      //   this.search()
      // }, 60000)
    },
    // beforeDestory() {
    //   clearInterval(this.interval)
    // },
    methods: {
      async handler({ BMap, map }) {
        this.BMap = BMap
        this.map = map
        const res = await companySetting()
        let currentCity = ''
        if (res.data.rows && res.data.rows.length > 0 && res.data.rows[0].city && res.data.rows[0].city !== '') currentCity = res.data.rows[0].city
        if (currentCity !== '') {
          map.centerAndZoom(currentCity, 14)
        } else {
          const localCity = new BMap.LocalCity()
          localCity.get(function(result) {
            const cityName = result.name
            map.centerAndZoom(cityName, 14)
          })
        }
      },
      search() {
        this.getCourierList()
      },
      // 单机单元格触发
      cellClick(row) {
        if (row.lat && row.lng) {
          this.getlocation(row)
        }
      },
      inputCheck() {
        if (this.qtype === 2) {
          this.txt = this.txt.replace(/[^\d]/g, '')
        }
      },
      getlocation(row) {
        var point = new this.BMap.Point(row.lng, row.lat)
        console.log(this.map)
        this.map.centerAndZoom(point, 20)

        this.infoWindowOpen2(row)
      },
      // 获取右边的table数据  配送员列表
      async getCourierList() {
        const params = { }
        if (this.qtype * 1 === 1) {
          params['userName'] = this.txt
        } else if (this.qtype * 1 === 2) {
          params['uphone'] = this.txt
          // if (this.txt.length !== 11) {
          //   this.$message.error('请输入正确的电话号码')
          //   return
          // }
        }
        const res = await courierList(params)
        this.tableDataRight.data = await res.data
        await this.getCourierMapList()
      },
      // 获取配送员的地图信息
      async getCourierMapList() {
        const params = { }
        const res = await courierMapList(params)
        const allMap = this.tableDataRight.data.map(item => {
          var data = res.data.find(i => item.userCode + '@' + item.corpCode === i.loginname)
          if (data) {
            1
          } else {
            data = { ...data }
          }
          return {
            ...item,
            ...data,
            createTime: data ? data.createTime : ''
          }
        })
        this.tableDataRight.data = [...allMap]
        // console.log(this.tableDataRight.data)
        const points = []
        this.tableDataRight.data.forEach((item) => {
          if (item.lat && item.lng) {
            points.push(item)
          }
        })
        this.markerList = points
      },
      infoWindowClose() {
        this.show = false
      },
      async infoWindowOpen(e) {
        console.log(e)
        const row = e.point
        this.getlocation(row)
        const params = {
          userId: row.userId
        }
        const res = await courierOrderwinData(params)
        this.tableData02.data = res.data.orderList
        this.tableData03.data = res.data.storeDetailList
        this.show = true
      },
      async infoWindowOpen2(row) {
        const params = {
          userId: row.userId
        }
        const res = await courierOrderwinData(params)
        this.tableData02.data = res.data.orderList
        this.tableData03.data = res.data.storeDetailList
        this.show = true
      },
      // 折叠效果
      fold(refItem) {
        const classname = event.target.className
        const flag = classname.includes('right')
        if (flag) {
          event.target.className = 'el-icon-d-arrow-left'
        } else {
          event.target.className = 'el-icon-d-arrow-right'
        }
        console.log(this.$refs[refItem].childNodes, 11111111)
        var width = this.$refs[refItem].style.width
        if (width === '20px') {
          this.$refs[refItem].style.width = '300px'
          this.$refs[refItem].childNodes[0].style.display = 'block'
          this.$refs[refItem].childNodes[4].style.display = 'block'
        } else {
          this.$refs[refItem].style.width = '20px'
          this.$refs[refItem].childNodes[0].style.display = 'none'
          this.$refs[refItem].childNodes[4].style.display = 'none'
        }
      }
    }
  }
</script>

<style lang="scss" scoped>
.peisong_main {
  display: flex;
  height: 100%;
  overflow-y: hidden;
  .form {
    .el-form-item {
      margin-bottom: 8px;
    }
  }
  .map {
    width: 100%;
    height: 100%;
  }
  .right {
    width: 320px;
    height: 100%;
    padding: 4px;
    transition: all 0.3s;
    .flex {
      display: flex;
      justify-content: space-between;
    }
    i {
      position: absolute;
      top: 50px;
      right: 0;
      cursor: pointer;
    }
    .table {
      width: 100%;
      height: calc(100% - 70px);
      overflow-y: hidden !important;
    }
    .rightCard {
      height: calc(100% - 22px);

      /deep/ .el-card__body {
        height: 100%;
        padding: 0;
      }
      /deep/ .el-card__header {
        height: 24px;
        padding: 0 10px;
        font-size: 14px;
        font-weight: bold;
        // padding: 0;
        line-height: 24px;
        color: #373A3C;
        background: #e3e3e3;
        border: 1px solid #ddd;
        box-sizing: border-box;
      }
    }
  }
}
.table {
  /deep/ .el-table__empty-block {
    width: auto;
  }
}
.info-box {
  position: absolute;
  top: 100px;
  left: 10px;
  width: 450px;
  height: 430px;
  padding: 20px;
  overflow-y: auto;
  background-color: white;
  .box-top {
    display: flex;
    justify-content: space-between;
    width: 100%;
    .el-icon-close {
      cursor: pointer;
    }
  }
}
#SGbaidumap {
  /*
  去除百度地图版权
  去除右上角[地图、卫星、三维]控件
  去除百度地图右上角平移缩放控件的市县区文字
  */
  >>> .anchorBL,
  >>> .anchorTR,
  >>> .BMap_zlHolder,
  >>> [id^=PanoramaFlashWraperTANGRAM] {
    display: none;
    visibility: hidden;
  }
}
</style>
